package com.sunfeng.n4.reentrant;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 验证可打断
 *
 * @author sunfeng
 * @date 2021/03/30 13:39
 **/

@Slf4j(topic = "c.ReentrantLockTest2")
public class ReentrantLockTest2 {

    private static ReentrantLock reentrantLock = new ReentrantLock();


    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                log.debug("尝试获取锁");
                reentrantLock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.debug("没有获取到锁  返回");
                return;
            }
            try {
                log.debug("获取到锁");
            } finally {
                reentrantLock.unlock();
            }

        }, "t1");
        reentrantLock.lock();

        thread.start();
        TimeUnit.SECONDS.sleep(1);
        log.debug("开始进行打断");

        thread.interrupt();
    }
}
